package com.cqemme.activitiservice.service.impl;

import com.cqemme.activitiservice.utils.EmptyChecker;
import com.cqemme.activitiservice.service.ActivitiService;
import com.cqemme.comm.utils.DateUtils;
import com.cqemme.comm.utils.JackSonUtils;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author nejery
 */
@Slf4j
@Service
public class ActivitiServiceImpl implements ActivitiService {


    @Resource
    private HistoryService historyService;


    @Override
    public Map<String, Object> selectInstanceInfo(String instanceId) {
        //查流程变量
        List<HistoricVariableInstance> hisVar = historyService.createHistoricVariableInstanceQuery().processInstanceId(instanceId).list();
        Map<String, Object> hisVarMap = new HashMap<>();
        for (HistoricVariableInstance his : hisVar) {
            hisVarMap.put(his.getVariableName(), his.getValue());
        }

        //查流程相关信息
        HistoricProcessInstance processInstance = historyService.createHistoricProcessInstanceQuery()
                .processInstanceId(instanceId).singleResult();
        hisVarMap.put("createTime", DateUtils.formatDateForStandard(processInstance.getStartTime()));
        hisVarMap.put("createTimeStr", DateUtils.calculateTime(processInstance.getStartTime()));
        hisVarMap.put("processName", processInstance.getProcessDefinitionName());

        //查历史流程节点
        List<HistoricActivityInstance> historicTaskInstanceList = historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(instanceId)
                .orderByHistoricActivityInstanceStartTime()
                .asc()
                .list();
        List<Map<String, Object>> hisTaskList = new ArrayList<>();
        Date startTime;
        for (HistoricActivityInstance historicTaskInstance : historicTaskInstanceList) {
            // 后面还有节点这个是定时任务产生的；
            startTime = historicTaskInstance.getStartTime();
            handleTaskData(historicTaskInstance,hisTaskList,startTime);
        }
        Map<String, Object> reMap = new HashMap<>(2);
        reMap.put("var", hisVarMap);
        reMap.put("hisTaskList", hisTaskList);
        return reMap;
    }


    private void handleTaskData(HistoricActivityInstance historicTaskInstance, List<Map<String, Object>> hisTaskList,Date startTime){
        Map<String, Object> hisMap = new HashMap<>();
        hisMap.put("taskId", historicTaskInstance.getTaskId());
        hisMap.put("taskKey", historicTaskInstance.getActivityId());
        hisMap.put("createTime", DateUtils.formatDateForStandard(startTime));
        hisMap.put("createTimeStr", DateUtils.calculateTime(startTime));
        Date endTime = historicTaskInstance.getEndTime();
        if (EmptyChecker.notEmpty(endTime)) {
            hisMap.put("endTime", DateUtils.formatDateForStandard(endTime));
            hisMap.put("endTimeStr", DateUtils.calculateTime(endTime));
        }
        hisMap.put("name", historicTaskInstance.getActivityName());
        hisMap.put("assignee", historicTaskInstance.getAssignee());
        hisMap.put("durationInMillis", historicTaskInstance.getDurationInMillis());
        hisTaskList.add(hisMap);
    }


}
